<?php
/**
 * Created by PhpStorm.
 * User: lzy 林子彦
 * Date: 2018/6/1
 * Time: 上午1:09
 */
namespace App\Admin\Controllers;
use App\Admin\Contracts\MenuInterface;
use App\Base\Controllers\Controller;
use Illuminate\Http\Request;


/**
 * 菜单控制类
 * Class MenuController
 * @package App\Admin\Controllers
 */
class MenuController extends Controller
{

    private  $formatTree;
    /**
     * 菜单接口
     * @return MenuInterface
     */
    public function getMenuInterface(){
        return app()->make(MenuInterface::class);
    }
    /**
     * 菜单
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
   public function index(){
       $menuList=$this->getMenuInterface()->getMenuList([]);
       $menuArr=array();
       foreach ( $menuList as $k=> $item) {
           $menuArr[$k]["id"]=$item->id;
           $menuArr[$k]["name"]=$item->title;
           $menuArr[$k]["pId"]=$item->pid;
           $menuArr[$k]["icon"]=$item->icon;
       }

       $tree = list_to_tree($menuList->toArray());
       $this->formatTree = array();
       $this->_toFormatTree($tree);
       //获取菜单选择框
       return view("admin.menu.index")->with("menu_list",json_encode($menuArr))->with("tree",$this->formatTree);
   }
    public function edit(Request $request){
          return $this->getMenuInterface()->getMenuInfo($request->all());
    }
    /**
     * 菜单保存
     */
   public function save(Request $request){
       return $this->getMenuInterface()->saveMenuData($request->all());
       return false;
   }

    /**
     * 菜单选择
     */
    public function show_select_menus(){
        $menuList=$this->getMenuInterface()->getMenuList([]);
        $menuArr=array();
        foreach ( $menuList as $k=> $item) {
            $menuArr[$k]["id"]=$item->id;
            $menuArr[$k]["name"]=$item->title;
            $menuArr[$k]["pId"]=$item->pid;
            $menuArr[$k]["icon"]=$item->icon;
        }

        return view("admin.menu.show_select_menus")->with("menu_list",json_encode($menuArr));
    }

    /**
     * 生成树形
     * @author 林子彦 <278805354@qq.com>
     * @param type $list
     * @param type $level
     * @param type $title
     * @return type
     */
    private function _toFormatTree($list, $level = 0, $title = 'title') {
        foreach ($list as $key => $val) {
            $tmp_str = str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;", $level * 2);
            $tmp_str .= "└";

            $val['level'] = $level;
            $val['title_show'] = $level == 0 ? $val[$title] . "&nbsp;" : $tmp_str . $val[$title] . "&nbsp;";
            // $val['title_show'] = $val['id'].'|'.$level.'级|'.$val['title_show'];
            if (!array_key_exists('_child', $val)) {
                array_push($this->formatTree, $val);
            } else {
                $tmp_ary = $val['_child'];
                unset($val['_child']);
                array_push($this->formatTree, $val);
                $this->_toFormatTree($tmp_ary, $level + 1, $title); //进行下一层递归
            }
        }
        return;
    }

}